﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：选择支出事项
 */
var selectedRow;
var refreshGirdData;
var acceptClick;
var dialogClose;

var keyword = request("keyword");
var selectValue = request('selectValue');
var dfopid = request('dfopid');

var firstLevel = request('firstLevel');

var bootstrap = function ($, learun) {
    "use strict";

    var page = {
        init: function () {
            page.bind();
            page.initGrid();
            page.search({keyword: keyword});
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            $("#txt_Keyword").keypress(function (even) {
                if (even.which == 13) {
                    var keyword = $(this).val();
                    page.search({keyword: keyword});
                }
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'basepayitems/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '编号', name: 'code', width: 100, align: 'left'},
                    {label: '名称', name: 'name', width: 225, align: 'left'},
                    {label: 'id', name: 'id', width: 1, align: 'left',hidden: true}
                ],
                dblclick: function (row) {
                    top.lrGirdLayerEditCallBack(row);

                    //关闭layer弹出层
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                },
                isPage: true,
                rows: 1000,
                reloadSelected: true,
                isMultiselect: true,
                isTree: false,
                mainId: 'id',
                onRenderComplete: function (rows) {
                    if (!!selectValue) {
                        var cb = ['checkbox_0.png', 'checkbox_1.png', 'checkbox_2.png'];
                        var imageurl = '/assets/plugins/images/jfgrid/';
                        var selectIds = selectValue.split(',');
                        var pageDatas = $('#gridtable').jfGridGet('rowdatas');
                        $.each($('#gridtable')[0].dfop.running.rowdata, function (_index, _item) {
                            var rownum = $('#gridtable').attr("id") + "_" + _index;
                            $.each(selectIds, function(_index2,_item2) {
                                if (_item['jfcheck'].value + '' == '0' && pageDatas[_index].id == _item2) {
                                    _item['jfcheck'].$cell.find('img').attr('src', imageurl + cb[1]);
                                    _item['jfcheck'].value = 1;
                                    $('#gridtable').find('[rownum="' + rownum + '"]').addClass('jfgrid-selected');
                                }
                            });
                        });
                    }

                }
            });
        },
        search: function (param) {
            param = param || {};
            param.status = 1;
            if (firstLevel) {
                param.firstLevel = firstLevel;
            }
            // param.keyword = keyword;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    //确认按钮
    acceptClick = function (callBack) {
        var value = $('#gridtable').jfGridValue('id');
        var selectDatas = $('#gridtable').jfGridGet('rowdata');
        if (learun.checkrow(value)) {
            var origArr = selectValue.split(',');
            var newArr = value.split(',');
            var origStr = origArr.sort().join();
            var newStr = newArr.sort().join();
            if(origStr != newStr) {
                callBack(selectDatas, dfopid);
            }
            return true;
        } else {
            return false;
        }
    }

    dialogClose = function () {
        try {
            var index = top.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            var $IsdialogClose = top.$("#layui-layer" + index).find('.layui-layer-btn').find("#IsdialogClose");
            var IsClose = $IsdialogClose.is(":checked");
            if ($IsdialogClose.length == 0) {
                IsClose = true;
            }
            if (IsClose) {
                top.layer.close(index);
            } else {
                location.reload();
            }
        } catch (e) {
            alert(e);
        }
    }

    page.init();
}

